package com.xiaoxi.bms.controller.menu;



import com.xiaoxi.bms.common.constant.constant.MenuCommonConstants;
import com.xiaoxi.bms.common.constant.constant.OperationConstants;
import com.xiaoxi.bms.common.constant.enumeration.statuscode.user.MenuUserEnums;
import com.xiaoxi.bms.common.exception.BMSException;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.xiaoxi.bms.domain.menu.vo.BMSMenuTree;
import com.xiaoxi.bms.domain.menu.vo.BMSMenuVO;
import com.xiaoxi.bms.domain.menu.vo.BMSMoveMenu;
import com.xiaoxi.bms.domain.menu.vo.BMSUserMenuTree;
import com.xiaoxi.bms.service.common.aspect.operation.OperationLog;
import com.xiaoxi.bms.service.menu.BMSMenuService;
import com.xiaoxi.bms.service.menu.BMSMoveMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoxi
 * @version 1.0
 * @date 2022/6/1 18:52
 */
@RestController
@RequestMapping("/menu/api/v1")
@Slf4j
public class BMSMenuController {

    @Resource
    private BMSMenuService bmsMenuService;

    @Resource
    private Map<String, BMSMoveMenuService> bmsMoveMenuServiceMap;

    @OperationLog(operation = OperationConstants.CODE_QUERY_TREE)
    @GetMapping("getMenuTree")
    public ApiResponse getMenuTree() {

        List<BMSMenuTree> bmsMenuTreeList = bmsMenuService.getMenuTree();
        return ApiResponse.ofSuccess(bmsMenuTreeList);

    }

    @OperationLog(operation = OperationConstants.CODE_MODIFY)
    @PostMapping("updateMenu")
    public ApiResponse updateMenu(@RequestBody BMSMenuVO bmsMenuVO) {

        try {
            bmsMenuService.updateMenu(bmsMenuVO);
        } catch (BMSException bmsException) {
            log.error("出现异常：{}", bmsException.getMessage());
            return ApiResponse.ofException(bmsException);
        }

        return ApiResponse.ofSuccess();

    }

    @OperationLog(operation = OperationConstants.CODE_MOVE)
    @PostMapping("moveMenu")
    public ApiResponse moveMenu(@RequestBody BMSMoveMenu bmsMoveMenu) {

        try {
            bmsMoveMenuServiceMap.get(MenuCommonConstants.MOVE_MENU + bmsMoveMenu.getMode()).move(bmsMoveMenu);
        } catch (BMSException bmsException) {
            log.error("出现异常：{}", bmsException.getMessage());
            return ApiResponse.ofException(bmsException);
        }

        return ApiResponse.ofSuccess();
    }

    @OperationLog(operation = OperationConstants.CODE_CREATE)
    @PostMapping("createMenu")
    public ApiResponse createMenu(@RequestBody BMSMenuVO bmsMenuVO) {

        try {
            bmsMenuService.createMenu(bmsMenuVO);
        } catch (BMSException bmsException) {
            log.error("出现异常：{}", bmsException.getMessage());
            return ApiResponse.ofException(bmsException);
        }

        return ApiResponse.ofSuccess();

    }

    @OperationLog(operation = OperationConstants.CODE_QUERY_ONE)
    @GetMapping("findOne/{id}")
    public ApiResponse findByOne(@PathVariable Long id) {

        BMSMenuVO bmsMenuVO = bmsMenuService.findByOne(id);
        return ApiResponse.ofSuccess(bmsMenuVO);

    }

    @OperationLog(operation = OperationConstants.CODE_DELETE)
    @GetMapping("deleteMenuById/{id}")
    public ApiResponse deleteMenuById(@PathVariable Long id) {

        bmsMenuService.deleteMenuById(id);
        return ApiResponse.ofSuccess();

    }

    @OperationLog(operation = OperationConstants.CODE_CHECK)
    @GetMapping("checkRouteName")
    public ApiResponse checkRouteName(String routeName, Long id) {

        Boolean repeat = bmsMenuService.checkRouteName(routeName, id);
        if (repeat) {
            return ApiResponse.ofEnum(MenuUserEnums.THE_ROUTE_NAME_ALREADY_EXISTS);
        }
        return ApiResponse.ofSuccess();

    }

    @OperationLog(operation = OperationConstants.CODE_CHECK)
    @GetMapping("checkTitleName")
    public ApiResponse checkTitleName(String titleName, Long id) {

        Boolean repeat = bmsMenuService.checkTitleName(titleName, id);
        if (repeat) {
            return ApiResponse.ofEnum(MenuUserEnums.THE_TITLE_NAME_ALREADY_EXISTS);
        }
        return ApiResponse.ofSuccess();

    }

    @OperationLog(operation = OperationConstants.CODE_QUERY_TREE)
    @GetMapping("getUserMenuTree")
    public ApiResponse getUserMenuTree() {

        List<BMSUserMenuTree> bmsMenuTreeList = bmsMenuService.getUserMenuTree();
        return ApiResponse.ofSuccess(bmsMenuTreeList);

    }


}
